import GameConfig from "../config/GameConfig";
import LanguageConfig from "../config/LanguageConfig";
import UserData from "../user/UserData";

/**
 * 辅助
 */
export default class Utils {
    /**
     * 遍历一级文件夹下的全部资源
     * @param url 一级路径
     * @param type 资源类型
     */
    static getFileByPath(url: string, type = cc.Asset): Array<string> {
        type = type || cc.Asset;
        let _url = url || "";
        let info = cc.resources["_config"].getDirWithPath(_url, type);
        let uuids = [];
        info.forEach((info) => {
            uuids.push(info.uuid)
        });
        //console.log("uuids:"+uuids);
        return uuids;
    }
    /**获取参数 */
    public static GetQueryValue1(queryName: string) {
        var reg = new RegExp("(^|&)" + queryName + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return decodeURI(r[2]);
        } else {
            return null;
        }
    }
    /**
     * 飘窗
     * @param content 内容
     * @param timeoutmills 延迟
     */
    public static toast(content: string, show = false, timeoutmills = 1): void {
        if (content == null) return;
        if (!show)
            //content = LanguageConfig.getText(content);
        cc.resources.load('prefab/toast', (err, prefab: cc.Prefab) => {
            let newNode: cc.Node = cc.instantiate(prefab);
            if (newNode) {
                newNode.zIndex = 20000;
                cc.director.getScene().getChildByName('Canvas').addChild(newNode);
                newNode.getChildByName('label').getComponent(cc.Label).string = content;
                newNode.opacity = 255;
                cc.tween(newNode).delay(0.5).to(0.5, { y: 100, opacity: 100 }).call(() => {
                    newNode.destroy();
                }).start();
            }
        });
    }
    /**缓存数据 */
    public static setItem(key: string, value: any) {
        if (typeof value == "object") value = JSON.stringify(value);
        key = GameConfig.gameName + "_" + UserData.userId + "_" + key;
        localStorage.setItem(key, value);
    }
    /**获取缓存数据 */
    public static getItem(key: string) {
        key = GameConfig.gameName + "_" + UserData.userId + "_" + key;
        let item = localStorage.getItem(key);
        return item;
    }
    /**获取变化次数 */
    public static getTimes(time: number = 1, dur: number = 0) {
        if (time < 0) {
            console.error("error time:" + time);
            return time;
        }
        let times = 500;
        if (time == 0) {//默认参数
            if (dur < 10) {
                time = 0.02;
            } else if (dur < 100) {
                time = 0.05;
            } else if (dur < 1000) {
                time = 0.08;
            } else if (dur < 10000) {
                time = 0.1;
            } else if (dur < 100000) {
                time = 0.2;
            } else if (dur < 1000000) {
                time = 0.3;
            } else {
                time = 0.5;
            }
        } else {
            times = 30;
        }
        return times * time;
    }
    /**获取某年某月的天数 */
    public static mGetDate(year: number, month: number) {
        var d = new Date(year, month, 0);
        return d.getDate();
    }
    /**
     * 时间格式化
     * @param time 时间 毫秒数
     * @param type 类型 转换类型 0=yy/mm/dd,1=yy-mm-dd,2=hh:mm:ss\nmm/dd,3:yy-mm-dd hh:mm (GMT+8:00)
     */
    public static getParseTime(time: number, type: number = 0) {
        let date = new Date(time);
        let year: any = date.getFullYear();
        let month: any = date.getMonth() + 1;
        month = month < 10 ? "0" + month : month
        let day: any = date.getDate();
        day = day < 10 ? "0" + day : day
        let hour: any = date.getHours();
        hour = hour < 10 ? "0" + hour : hour
        let minute: any = date.getMinutes();
        minute = minute < 10 ? "0" + minute : minute
        let second: any = date.getSeconds();
        second = second < 10 ? "0" + second : second;
        if (type == 0) {
            return year + "/" + month + "/" + day;
        } else if (type == 1) {
            return year + "-" + month + "-" + day;
        } else if (type == 2) {
            return hour + ":" + minute + ":" + second + "\n" + month + "/" + day;
        } else if (type == 3) {
            return year + "/" + month + "/" + day + " " + hour + ":" + minute + " (GMT+7:00)";
        } else if (type == 4) {
            return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
        }
    }
    /**获取图集 */
    public static getAtlas(spriteAtlasKey: string, callBack: Function, language = LanguageConfig.gameLanguage) {
        if (!spriteAtlasKey) return;
        let path = LanguageConfig.languagePath[language];
        let filePath = "i18n/" + path + "/" + spriteAtlasKey;
        cc.resources.load(filePath, cc.SpriteAtlas, null, (error, asset: cc.SpriteAtlas) => {
            if (error) return console.error("load error path:" + filePath);
            callBack(asset);
        });
    }

    /**格式化金币 */
    public static parseCoin(coin: number) {
        let fixed = GameConfig.versionType == 2 ? 0 : 2;
        if (coin < 1000) return "" + coin.toFixed(fixed);
        let num = Math.floor(coin).toString(), result = '';
        let last = coin.toFixed(fixed).split(".")[1];
        while (num.length > 3) {
            result = ',' + num.slice(-3) + result;
            num = num.slice(0, num.length - 3);
        }
        if (num) { result = num + result; }
        return result + (fixed ? ("." + last) : "");
    }


}
